package com.enjoy.dome.manage;

import android.app.Activity;
import android.app.Service;
import android.content.Context;

import java.util.LinkedList;

/**
 * description: Activity管理类
 */
public class AppManager {
    private static LinkedList<Activity> activityStack = null;
    private static LinkedList<Service> serviceStack = null;
    private static AppManager instance;

    /**
     * 单一实例
     */
    public static AppManager getInstance() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 对比Activity
     */
    public boolean contains(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null)
            activityStack = new LinkedList<Activity>();
        activityStack.add(activity);
    }

    /**
     * 删除指定Activity
     */
    public void delActivity(Activity activity) {
        if (activityStack == null)
            return;
        activityStack.remove(activity);
    }

    /**
     * 删除除栈顶的所有activity
     */
    public void delActivity2() {
        if (activityStack == null)
            return;
        for (int i = 0; i < activityStack.size() - 1; i++) {
            activityStack.get(i).finish();
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    @Deprecated
    private Activity currentActivity() {
        if (activityStack == null)
            return null;
        Activity activity = activityStack.getLast();
        return activity;
    }

    /**
     * 从堆栈中取出指定类名的activity 没有则返回null
     */
    public Activity getActivity(Class<? extends Activity> cls) {
        for (int i = activityStack.size() - 1; i > 0; i--) {
            if (activityStack.get(i).getClass().equals(cls)) {
                return activityStack.get(i);
            }
        }
        return null;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    @Deprecated
    private void finishCurrentActivity() {
        Activity activity = activityStack.getLast();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.getLast();
        finishActivity(activity);
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (int i = activityStack.size() - 1; i > 0; i--) {
            if (activityStack.get(i).getClass().equals(cls)) {
                finishActivity(activityStack.get(i));
            }
        }
    }

    /**
     * 结束所有的Activity 只剩下指定的activity
     */
    public void finishAllActivity(Class<?> cls, Class<?> cls2) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (null != activityStack.get(i) && !activityStack.get(i).getClass().equals(cls)
                    && !activityStack.get(i).getClass().equals(cls2)
            ) {
                finishActivity(activityStack.get(i));
            }
        }
        finishActivity(cls2);
    }

    /**
     * 结束所有的Activity 只剩下指定的activity
     */
    public void finishAll(Class<?> cls) {
        for (int i = activityStack.size() - 1; i > 0; i--) {
            if (!activityStack.get(i).getClass().equals(cls)) {
                finishActivity(activityStack.get(i));
            }
        }
    }

    /**
     * 结束所有的Activity 只剩下指定的activity集合
     */
    public void finishAll(Class<?>... classes) {
        for (int i = activityStack.size() - 1; i > 0; i--) {
            boolean is = false;
            for (Class<?> cla : classes) {
                if (activityStack.get(i).getClass().equals(cla)) {
                    is = true;
                }
            }
            if (!is) {
                finishActivity(activityStack.get(i));
            }
        }
    }


    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (serviceStack != null) {
            for (int i = 0; i < serviceStack.size(); i++) {
                if (null != serviceStack.get(i)) {
                    serviceStack.get(i).stopSelf();
                }
            }
            serviceStack.clear();
        }
        if (activityStack != null) {
            for (int i = 0; i < activityStack.size(); i++) {
                if (null != activityStack.get(i)) {
                    activityStack.get(i).finish();
                }
            }
            activityStack.clear();
        }
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            //只退出应用不再结束进程
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
        }
    }
}
